package com.hzpd.hainan.services;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.hzpd.hainan.api.InterfaceJsonfile;
import com.hzpd.hainan.app.App;
import com.hzpd.hainan.model.news.NewsChannelBean;
import com.hzpd.hainan.model.news.NewsChannelEntity;
import com.hzpd.hainan.model.newspager.NewsPaperNode;
import com.hzpd.hainan.model.newspager.NodeEntity;
import com.hzpd.hainan.model.welcome.ImageEntity;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;
import com.hzpd.hainan.utils.SPUtil;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/6/30.
 */

public class InitService extends IntentService {
    public static final String InitAction = "initServer";

    public InitService() {
        super("InitService");
    }
    public Boolean isUpdata=false;

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.i("oncreate");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (intent!=null){
            if (InitAction.equals(intent.getAction())){
//                getWelcomePic();
//                getChannelJson();
                // 数字报
//            getNewsPaperNode();
            }
        }
    }

    /**
     * 开屏图获取
     */
    private void getWelcomePic() {
        Map<String, String> map = new HashMap<>();
        map.put("siteid", InterfaceJsonfile.SITEID);
        Factory.provideHttpService()
                .welcomeAd(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ImageEntity>() {
                    @Override
                    public void onCompleted() {
                        LogUtils.i("onCompleted-->");
                    }
                    @Override
                    public void onError(Throwable e) {
                        LogUtils.i("error-->");
                        SPUtil.getInstance().setWelcome("");
                        e.printStackTrace();
                    }
                    @Override
                    public void onNext(ImageEntity entity) {
                        LogUtils.i("onnext-->"+entity.data.getImgurl());
                        StringBuilder sb = new StringBuilder();
                        for (String imgurl:entity.data.getImgurl()) {
                            sb.append(imgurl);
                            sb.append(",");
                        }
                        String imgStr = sb.toString();
                        SPUtil.getInstance().setWelcome(imgStr);
                    }
                });
    }

    // 数字报
    private void getNewsPaperNode() {
        String pagerRootpath = App.getInstance().getJsonFileCacheRootDir() + File.separator + "newspager"
                + File.separator;
        Factory.SZBprovideHttpService().downloadNodeFile(InterfaceJsonfile.NEWSPAGER)
                .subscribeOn(Schedulers.io())
                .map(new Func1<NodeEntity, Boolean>() {
                    @Override
                    public Boolean call(NodeEntity entity) {
                        List<NewsPaperNode> list = entity.data;
                        if (list != null && list.size() > 0) {
                            App.nodeList = entity;
                            App.node = App.nodeList.data.get(0);
                        }
                        LogUtils.i("newspager-->" + App.node.getNodeid());
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean b) {

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("newspager-->3"+throwable);
                    }
                });
    }
}
